<template>
  <div class="login-bg">
    <div class="login">
      <div class="message">管理登录</div>
      <div class="shadow"></div>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input placeholder="请输入账号" prefix-icon="fa fa-user-o" v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="请输入密码" prefix-icon="fa fa-lock" show-password v-model="form.password">
          </el-input>
        </el-form-item>
        <el-form-item style="float:right">
          <el-button type="primary" @click="submit('form')">登录</el-button>
          <el-button @click="reset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="bottom-bar"><p>成都市金联讯科技有限公司</p><p>蜀ICP备20003233号</p></div>
  </div>
</template>
<script>
import { login } from '@/network/api.js'
export default {
  name: 'login',
  data() {
    return {
      form: {
        password: '123456',
        account: 'admin'
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'change' }]
      }
    }
  },
  created() {
    this.$storage.clear()
  },
  methods: {
    // 提交表单
    submit(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return false
        try {
          const rtn = await login(this.form)
          this.$storage.set('token', rtn.token, rtn.expire)
          this.$router.push('/home')
        } catch (e) {
          this.$message.error(e.message)
        }
      })
    },
    // 重置表单
    reset(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style  scoped>
.bottom-bar{
  position: absolute;
  top: 90%;
  width: 100%;
  height: 10%;
  text-align: center;
  font-size: 1rem;
  color: white;
}
</style>
